﻿#region NeoPatterns
// 
// NeoPatterns framework is released under LGPLv3 license.
// See http://www.opensource.org/licenses/lgpl-3.0.html for more details.
// 
// Please visit http://neopatterns.wikidot.com for more information about NeoPatterns
// 
// Copyright (c) Pascal Craponne 2008
// 
#endregion

using System;
using System.Reflection;

namespace NeoPatterns.Utility
{
    public static class ParameterInfoExtension
    {
        /// <summary>
        /// Determines if a parameter is passed by ref (a structure/value)
        /// </summary>
        /// <param name="parameterInfo"></param>
        /// <returns></returns>
        public static bool IsRef(this ParameterInfo parameterInfo)
        {
            return parameterInfo.ParameterType.IsByRef;
        }

        /// <summary>
        /// Returns the type in a safe way, treating ref parameters correctly
        /// </summary>
        /// <param name="parameterInfo"></param>
        /// <returns></returns>
        public static Type GetParameterType(this ParameterInfo parameterInfo)
        {
            if (IsRef(parameterInfo))
                return parameterInfo.ParameterType.GetElementType();
            return parameterInfo.ParameterType;
        }
    }
}
